package com.adee.design.structure.adapter;

/*
 * 适配器模式：
 * 角色：目标接口，现有类，适配器类
 * 举例：电脑电源就是一个适配器，将120v电压转为5v。
 * */
public class Test {
    public static void main(String[] args) {
        Power120v power120v = new Power120v();
        PowerAdapter adapter = new PowerAdapter();
        adapter.setPower120v(power120v);
        adapter.provide5v();
    }
}

// 目标接口
interface Power5v {
    void provide5v();
}

// 现有类
class Power120v {
    void provide120v() {
        System.out.println("提供120v电压");
    }
}

// 适配器类
class PowerAdapter implements Power5v {
    private Power120v power120v;
    public void setPower120v(Power120v power120v) {
        this.power120v = power120v;
    }
    @Override
    public void provide5v() {
        power120v.provide120v();
        System.out.println("电源适配器将电压由120v转为5v");
        System.out.println("提供5v电压");
    }
}


